from random import randint
from django.http.response import Http404, HttpResponse
from django.shortcuts import render
from rest_framework.views import APIView
from django_redis import get_redis_connection
# from ronglian_sms_sdk import SmsSDK
import logging

logger = logging.getLogger("django")

# sdk = SmsSDK("8aaf07087955b4b50179a8c0936c20c4", "8a74403030db430cb139032c82948eb5", "8aaf07087955b4b50179a8c0944420cb")


class SMSCodeView(APIView):
    """短信验证码"""

    def get(self, request, mobile):
        """return conde"""
        sms_code = "%06d" % (randint(0, 999999))
        redis_conn = get_redis_connection("verify_codes")
        redis_conn.setex("sms_%s" % mobile, 300, sms_code)
        # 开发过程中不必发送sms_code，打印到后台方便验证
        # sdk.sendMessage('1', '18524438605', (sms_code,))
        print("-" * 33 + " sms_code " + sms_code + " " + "-" * 44)
        return HttpResponse(sms_code)
